// let nativeBridge = require('./nativeBridge');
// let wondersWeChat = require('./wondersWeChat');
import nativeBridge from './nativeBridge'
import wondersWeChat from './wondersWeChat'
// (function (window) {
function WondersApp() {}
let u = navigator.userAgent.toLowerCase();
//url参数获取
function getQueryVariable(variable) {
	let query = window.location.href.split("?");
	let vars = query[1] && query[1].split("&");
	if (vars && vars.length) {
		for (let i = 0; i < vars.length; i++) {
			let pair = vars[i].split("=");
			if (pair[0] == variable) {
				return pair[1];
			}
		}
	}
	return false;
}
function setNativeStorage(data) {
	let key = data.key;
	if (data.method == "set") {
		localStorage[key] = data.value;
	} else if (data.method == "delete") {
		localStorage.removeItem(key);
	}
}
WondersApp.prototype.initUrlStorage = function () {
	//微信小程序里需要取url中的参数，再存储
	let weiXinDATA = JSON.parse(decodeURIComponent(getQueryVariable("weiXinDATA")));
	let wxData = null;
	if (weiXinDATA) {
		if (typeof weiXinDATA === "string") {
			weiXinDATA = JSON.parse(weiXinDATA);
		}
		if (weiXinDATA && weiXinDATA.length) {
			weiXinDATA.forEach(element => {
				wxData = {
					key: element["key"],
					value: element["value"],
					method: element["method"] ? element["method"] : "set"
				};
				setNativeStorage(wxData);
			});
		}
	}
};

// 环境判断
WondersApp.prototype.QuickVersion = {
	init: WondersApp.prototype.initUrlStorage(),
	isWeixin: u.indexOf("micromessenger") != -1,
	isWondersMobile: u.indexOf("wonders_mobile") != -1 || u.indexOf("health_hainan") != -1 || u.indexOf("medical_community") != -1 //通用
};
//打开地图导航
WondersApp.prototype.showMapRoute = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.showMapRoute(data, callback);
	} else if (this.QuickVersion.isWeixin) {
		wondersWeChat.showMapRoute(data, callback);
	} else {
		window.location.href =
			"https://uri.amap.com/navigation?from=" +
			data.sLon +
			"," +
			data.sLat +
			",当前位置&to=" +
			data.tLon +
			"," +
			data.tLat +
			"," +
			data.sName +
			"&src=mypage";
	}
};
//微信支付
WondersApp.prototype.wxPay = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.wxPay(data, callback);
	} else if (this.QuickVersion.isWeixin) {
		wondersWeChat.wxPay(data, callback);
	} else {
		console.log("请在APP端调用");
	}
};
//二维码
WondersApp.prototype.qrCodeScan = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		console.log('111111');
		nativeBridge.qrCodeScan(data, callback);
	} else if (this.QuickVersion.isWeixin) {
		wondersWeChat.wondersWxJsSDKApi("qrCodeScan", data, callback);
	} else {
		console.log("请在APP端调用");
	}
};
//打电话、短信
WondersApp.prototype.callPhone = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.callPhone(data, callback);
	} else {
		wondersWeChat.callPhone(data, callback);
	}
};
//获取版本号
WondersApp.prototype.getVersion = function (data) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.getVersion(data);
	} else {
		console.log("请在APP端调用");
	}
};
//分享
WondersApp.prototype.share = function (data) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.share(data);
	} else {
		console.log("请在APP端调用");
	}
};
//获取经纬度
WondersApp.prototype.getLocation = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.getLocation(data, callback);
	} else if (this.QuickVersion.isWeixin) {
		wondersWeChat.wondersWxJsSDKApi("getLocation", data, callback);
	} else {
		// 高德地图依赖
		let url = "https://webapi.amap.com/maps?v=1.4.15&key=863ce4441eb85cb737d36126aac8227d&callback=onMapLoad";
		let jsapi = document.createElement("script");
		jsapi.charset = "utf-8";
		jsapi.src = url;
		document.head.appendChild(jsapi);
		console.log("获取经纬度..");
		window.onMapLoad = function () {
			// var map = new AMap.Map('container');
			AMap.plugin("AMap.Geolocation", function () {
				let geolocation = new AMap.Geolocation({
					// 是否使用高精度定位，默认：true
					enableHighAccuracy: true,
					// 设置定位超时时间，默认：无穷大
					timeout: 10000,
					// 定位按钮的停靠位置的偏移量，默认：Pixel(10, 20)
					buttonOffset: new AMap.Pixel(10, 20),
					//  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
					zoomToAccuracy: true,
					//  定位按钮的排放位置,  RB表示右下
					buttonPosition: "RB"
				});

				geolocation.getCurrentPosition();
				AMap.event.addListener(geolocation, "complete", onComplete);
				AMap.event.addListener(geolocation, "error", onError);

				function onComplete(data) {
					console.log("data是具体的定位信息", data.position);
					let backData = {
						code: 0,
						msg: "",
						longitude: data.position.lng,
						latitude: data.position.lat
					};
					console.log("backData", backData);
					callback(backData);
				}

				function onError(data) {
					callback(data);
					console.log("定位出错", data);
				}
			});
		};
	}
};
// 选择图片
WondersApp.prototype.chooseImage = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.chooseImage(data, callback);
	} else if (this.QuickVersion.isWeixin) {
		wondersWeChat.wondersWxJsSDKApi("chooseImage", data, callback);
	} else {
		console.log("请在APP端调用");
	}
};
// 调用相机
WondersApp.prototype.camera = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.camera(data, callback);
	} else if (this.QuickVersion.isWeixin) {
		wondersWeChat.wondersWxJsSDKApi("camera", data, callback);
	} else {
		console.log("请在APP端调用");
	}
};
// 信息存储
WondersApp.prototype.nativeStorage = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.nativeStorage(data, callback);
	} else {
		let key = data.key;
		let result = {
			code: 102,
			msg: "未定义方法"
		};
		if (data.method == "get") {
			result = {
				code: 0,
				msg: "",
				[key]: localStorage[key]
			};
		} else if (data.method == "set") {
			localStorage[key] = data.value;
			result = {
				code: 0,
				msg: ""
			};
		} else if (data.method == "delete") {
			localStorage.removeItem(key);
			result = {
				code: 0,
				msg: ""
			};
		}
		callback(result);
	}
};
// 原生语音
WondersApp.prototype.nativeVioce = function (callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.nativeVioce(callback);
	} else {
		console.log("请在APP端调用");
	}
};
// 原生搜索
WondersApp.prototype.nativeSearchVal = function (data) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.nativeSearchVal(data);
	} else {
		console.log("请在APP端调用");
	}
};
// 调用原生认证方式
WondersApp.prototype.authentication = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.authentication(data, callback);
	} else {
		console.log("请在APP端调用");
	}
};
// 获取原生支持的方式
WondersApp.prototype.authType = function (callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.authType(callback);
	} else {
		console.log("请在APP端调用");
	}
};
// 横竖屏
WondersApp.prototype.rotate = function (data) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.rotate(data);
	} else {
		console.log("请在APP端调用");
	}
};
// 下载
WondersApp.prototype.download = function (data) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.download(data);
	} else {
		console.log("请在APP端调用");
	}
};
// 获取本地缓存大小
WondersApp.prototype.cacheSize = function (callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.cacheSize(callback);
	} else {
		console.log("请在APP端调用");
	}
};
// 清除缓存
WondersApp.prototype.clearCache = function (callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.clearCache(callback);
	} else {
		console.log("请在APP端调用");
	}
};
// 消息通知
WondersApp.prototype.newMessage = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.newMessage(data, callback);
	} else {
		console.log("请在APP端调用");
	}
};
// 视频
WondersApp.prototype.video = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.video(data, callback);
	} else {
		console.log("请在APP端调用");
	}
};
// 刷新token
WondersApp.prototype.refreshToken = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.refreshToken(data, callback);
	} else {
		console.log("请在APP端调用");
	}
};
// 角色切换
WondersApp.prototype.changeRole = function () {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.changeRole();
	} else {
		console.log("请在APP端调用");
	}
};
// 推出登录
WondersApp.prototype.logout = function () {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.logout();
	} else {
		console.log("请在APP端调用");
	}
};
// 底部设置
WondersApp.prototype.tabConfig = function (data) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.tabConfig(data);
	} else {
		console.log("请在APP端调用");
	}
};
//头部设置
WondersApp.prototype.setHeader = function (data) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.setHeader(data);
	} else {
		console.log("请在APP端调用");
	}
};
// 页面即将出现
WondersApp.prototype.onVisible = function (callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.onVisible(callback);
	}
};
// 页面即将消失
WondersApp.prototype.onInvisible = function (callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.onInvisible(callback);
	}
};
// 页面即将销毁
WondersApp.prototype.onDestory = function (callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.onDestory(callback);
	}
};

/*
 *跳转原生页面
 * data.animate String push、present
 * data.toPage String 页面，值由h5与前端商议确定
 * data.params Json 页面参数，字段由h5与前端商议确定
 * callback 回调，具体回调时机，由h5与前端商议确定
 */
WondersApp.prototype.forwardNative = function (data, callback) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.forwardNative(data, callback);
	} else {
		console.log("请在APP端调用");
	}
};

WondersApp.prototype.loginIM = function () {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.loginIM();
	} else {
		console.log("请在APP端调用");
	}
};

// 原生页面跳转
WondersApp.prototype.toNative = function (router, nativeData, refreshUrl, animate, hasNavigation, float) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.toNative(router, nativeData, refreshUrl, animate, hasNavigation, float);
	}
};
// 页面返回
WondersApp.prototype.toBack = function (url, nativeData, refreshUrl, hasNavigation, float) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.toBack(url, nativeData, refreshUrl, hasNavigation, float);
	} else {
		if (url) {
			window.location.href = url;
		} else {
			window.history.back(-1);
		}
	}
};
// 打开新页面跳转
WondersApp.prototype.toPage = function (url, nativeData, hasNavigation, float) {
	if (this.QuickVersion.isWondersMobile) {
		nativeBridge.toPage(url, nativeData, hasNavigation, float);
	} else {
		window.location.href = url;
	}
};
//模块导出或挂载到 window
let wondersApp = new WondersApp();
export default wondersApp;
// if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
// module.exports = wondersApp;
// } else {
//   window.WondersApp = wondersApp;
// }
// })(window);
